package thread;

public class SyncDemo2 {
    public static void main(String[] args) {
        Shop shop1 = new Shop();
        Shop shop2 = new Shop();
        new Thread(shop1::buy).start();
        new Thread(shop2::buy).start();
    }
}

class Shop {
    public void buy() {
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + "正在挑衣服");
            Thread.sleep(2000);

            synchronized (this) {
                System.out.println(t.getName() + "正在试衣服");
                Thread.sleep(2000);

            }
            System.out.println(t.getName() + "买完了衣服");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
